package com.llxz.blog.ucenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llxz.blog.ucenter.entity.User;
import com.llxz.blog.ucenter.entity.vo.ChangePasswordForm;
import com.llxz.blog.ucenter.entity.vo.RegisterForm;
import com.llxz.blog.ucenter.service.UserService;
import com.llxz.blog.util.JwtUtils;
import com.llxz.blog.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author lyq
 * @description: 用户中心控制器
 * date: 2021/9/4 15:33
 */

@Api(tags = "前台用户")
@RestController
@RequestMapping("/ucenter/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("logout")
    public R logout(@RequestBody User user) {
        try {
            System.out.println(user.getMobile() + "controller ============");
            userService.subUserStatus(user);
            return R.ok();
        } catch (Exception e) {
            return R.error().message(e.getMessage());
        }
    }

    @ApiModelProperty(value = "登录")
    @PostMapping("login")
    public R login(@RequestBody User user) {
        String token = userService.login(user);
        String id = JwtUtils.getData(token);
        User user1 = userService.getById(id);
        if(user1.getAvatar().equals("http://www.baidu.com")){
            user1.setAvatar("beijing.aliyuncs.com/2021/09/10/606b97cfc4d14ba691f8990694f61ec3mmexport1630649295855.jpg");
        }
//        System.out.println("user :" + user1 );
        final R userInfo = R.ok().data("userInfo", user1);
        userInfo.data("token", token);
        return userInfo;
    }

    @ApiModelProperty("注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterForm registerForm) {
        userService.register(registerForm);
        return R.ok().message("注册成功");
    }

    //根据token获取用户信息
    @ApiModelProperty("获取用户详细信息")
    @PostMapping("getInfo")
    public R getInfo(@RequestParam String token) {
        //查询数据库根据用户id获取用户信息
        String id = JwtUtils.getData(token);
        User user = userService.getById(id);
        return R.ok().data("userInfo", user);
    }
//ucenter/user/changePassword
    //修改密码
    @ApiModelProperty("修改密码")
    @PostMapping("changePassword")
    public R changePassword(@RequestBody ChangePasswordForm changePasswordForm) {
        userService.changePassword(changePasswordForm);
        return R.ok().message("修改密码成功");
    }

    //注销
    @ApiModelProperty("注销")
    @DeleteMapping("cancel")
    public R cancel(String mobile, String password) {
        userService.delete(mobile, password);
        return R.ok().message("注销成功");
    }

    @ApiModelProperty("更新个人信息")
    @PostMapping("update")
    public R update(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", user.getMobile());
        User one = userService.getOne(wrapper);
        user.setId(one.getId());
        user.setPassword(one.getPassword());
        userService.updateById(user);
        return R.ok().message("更新成功");
    }
}

